

import 'dart:typed_data';

import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

abstract class ImageLoadable{
  /// Creates a widget that displays an [ImageStream] obtained from an asset
  /// bundle. The key for the image is given by the `name` argument.
  ///
  /// [compressionRatio] 图片压缩率,取值范围[0, 1], null不压缩
  /// [clearMemoryCacheWhenDispose] 如果图片从 tree 中移除，是否清掉内存缓存
  ExtendedImage asset(
      String name, {
        Key? key,
        AssetBundle? bundle,
        double? scale,
        double? width,
        double? height,
        Color? color,
        BlendMode? colorBlendMode,
        BoxFit? fit,
        Alignment alignment = Alignment.center,
        ImageRepeat repeat = ImageRepeat.noRepeat,
        BoxShape? shape,
        BoxBorder? border,
        BorderRadius? borderRadius,
        double? compressionRatio,
        bool clearMemoryCacheWhenDispose = false,
      }) {
    return ExtendedImage.asset(
      name,
      key: key,
      bundle: bundle,
      scale: scale,
      width: width,
      height: height,
      color: color,
      colorBlendMode: colorBlendMode,
      fit: fit,
      alignment: alignment,
      repeat: repeat,
      shape: shape,
      border: border,
      borderRadius: borderRadius,
      compressionRatio: compressionRatio,
      clearMemoryCacheWhenDispose: clearMemoryCacheWhenDispose,
    );
  }

  /// 网络图片加载
  ExtendedImage network(
      String url, {
        Key? key,
        double? width,
        double? height,
        Color? color,
        BlendMode? colorBlendMode,
        BoxFit? fit,
        Alignment alignment = Alignment.center,
        ImageRepeat repeat = ImageRepeat.noRepeat,
        BoxShape? shape,
        BoxBorder? border,
        BorderRadius? borderRadius,
        double? compressionRatio,
        bool clearMemoryCacheWhenDispose = false,
        Widget? loadingWidget,
        Widget? loadFailedWidget,
      }) {
    return ExtendedImage.network(
      url,
      key: key,
      width: width,
      height: height,
      color: color,
      colorBlendMode: colorBlendMode,
      fit: fit,
      alignment: alignment,
      repeat: repeat,
      shape: shape,
      border: border,
      borderRadius: borderRadius,
      compressionRatio: compressionRatio,
      clearMemoryCacheWhenDispose: clearMemoryCacheWhenDispose,
      loadStateChanged: (state) {
        if (state.extendedImageLoadState == LoadState.loading)
          return loadingWidget;
        if (state.extendedImageLoadState == LoadState.failed)
          return loadFailedWidget;
        return null;
      },
    );
  }

  /// Creates a widget that displays an [ImageStream] obtained from a [Uint8List].
  ///
  /// The [bytes], [scale], and [repeat] arguments must not be null.
  ///
  /// Either the [width] and [height] arguments should be specified, or the
  /// widget should be placed in a context that sets tight layout constraints.
  /// Otherwise, the image dimensions will change as the image is loaded, which
  /// will result in ugly layout changes.
  ExtendedImage memory(
      Uint8List bytes, {
        Key? key,
        double scale = 1.0,
        double? width,
        double? height,
        Color? color,
        BlendMode? colorBlendMode,
        BoxFit? fit,
        Alignment alignment = Alignment.center,
        ImageRepeat repeat = ImageRepeat.noRepeat,
        BoxShape? shape,
        BoxBorder? border,
        BorderRadius? borderRadius,
        double? compressionRatio,
        bool clearMemoryCacheWhenDispose = false,
      }) {
    return ExtendedImage.memory(
      bytes,
      key: key,
      scale: scale,
      width: width,
      height: height,
      color: color,
      colorBlendMode: colorBlendMode,
      fit: fit,
      alignment: alignment,
      repeat: repeat,
      shape: shape,
      border: border,
      borderRadius: borderRadius,
      compressionRatio: compressionRatio,
      clearMemoryCacheWhenDispose: clearMemoryCacheWhenDispose,
    );
  }

  /// Creates a widget that displays an [ImageStream] obtained from a [File].
  ///
  /// The [file], [scale], and [repeat] arguments must not be null.
  ///
  /// Either the [width] and [height] arguments should be specified, or the
  /// widget should be placed in a context that sets tight layout constraints.
  /// Otherwise, the image dimensions will change as the image is loaded, which
  /// will result in ugly layout changes.
  ExtendedImage file(
      File file, {
        Key? key,
        double scale = 1.0,
        double? width,
        double? height,
        Color? color,
        BlendMode? colorBlendMode,
        BoxFit? fit,
        Alignment alignment = Alignment.center,
        ImageRepeat repeat = ImageRepeat.noRepeat,
        BoxShape? shape,
        BoxBorder? border,
        BorderRadius? borderRadius,
        double? compressionRatio,
        bool clearMemoryCacheWhenDispose = false,
      }) {
    return ExtendedImage.file(
      file,
      key: key,
      scale: scale,
      width: width,
      height: height,
      color: color,
      colorBlendMode: colorBlendMode,
      fit: fit,
      alignment: alignment,
      repeat: repeat,
      shape: shape,
      border: border,
      borderRadius: borderRadius,
      compressionRatio: compressionRatio,
      clearMemoryCacheWhenDispose: clearMemoryCacheWhenDispose,
    );
  }
}